Completed
Pull Request — future (#174)
by Xaver
30s
created

proportions.js ➔ ... ➔ hostnameOfNodeID   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 1
1
define(['d3-interpolate', 'snabbdom', 'filters/genericnode', 'helper'],
2
  function (d3Interpolate, V, Filter, helper) {
3
    'use strict';
4
5
    return function (config, filterManager) {
6
      var self = this;
7
      var scale = d3Interpolate.interpolate('#770038', '#dc0067');
8
      V = V.default;
9
10
      var statusTable;
11
      var fwTable;
12
      var hwTable;
13
      var geoTable;
14
      var autoTable;
15
      var gatewayTable;
16
      var gateway6Table;
17
      var siteTable;
18
19
      function count(nodes, key, f) {
20
        var dict = {};
21
22
        nodes.forEach(function (d) {
23
          var v = helper.dictGet(d, key.slice(0));
24
25
          if (f !== undefined) {
26
            v = f(v);
27
          }
28
29
          if (v === null) {
30
            return;
31
          }
32
33
          dict[v] = 1 + (v in dict ? dict[v] : 0);
34
        });
35
36
        return Object.keys(dict).map(function (d) {
37
          return [d, dict[d], key, f];
38
        });
39
      }
40
41
      function addFilter(filter) {
42
        return function () {
43
          filterManager.addFilter(filter);
44
          return false;
45
        };
46
      }
47
48
      function fillTable(name, table, data) {
49
        if (!table) {
50
          table = document.createElement('table');
51
        }
52
53
        var max = Math.max.apply(Math, data.map(function (o) {
54
          return o[1];
55
        }));
56
57
        var items = data.map(function (d) {
58
          var v = d[1] / max;
59
60
          var filter = new Filter(_.t(name), d[2], d[0], d[3]);
61
62
          var a = V.h('a', { props: { href: '#' }, on: { click: addFilter(filter) } }, d[0]);
63
64
          var th = V.h('th', a);
65
          var td = V.h('td', V.h('span', {
66
            style: {
67
              width: Math.round(v * 100) + '%',
68
              backgroundColor: scale(v),
69
              color: 'white'
70
            }
71
          }, d[1].toFixed(0)));
72
73
          return V.h('tr', [th, td]);
74
        });
75
        var tableNew = V.h('table', { props: { className: 'proportion' } }, items);
76
        return V.patch(table, tableNew);
77
      }
78
79
      self.setData = function setData(data) {
80
        var onlineNodes = data.nodes.all.filter(helper.online);
81
        var nodes = onlineNodes.concat(data.nodes.lost);
82
83
        function hostnameOfNodeID(nodeid) {
84
          var gateway = data.nodeDict[nodeid];
85
          if (gateway) {
86
            return gateway.hostname;
87
          }
88
          return null;
89
        }
90
91
        var gatewayDict = count(nodes, ['gateway'], hostnameOfNodeID);
92
        var gateway6Dict = count(nodes, ['gateway6'], hostnameOfNodeID);
93
94
        var statusDict = count(nodes, ['is_online'], function (d) {
95
          return d ? 'online' : 'offline';
96
        });
97
        var fwDict = count(nodes, ['firmware', 'release']);
98
        var hwDict = count(nodes, ['model']);
99
        var geoDict = count(nodes, ['location'], function (d) {
100
          return d && d.longitude && d.latitude ? _.t('yes') : _.t('no');
101
        });
102
103
        var autoDict = count(nodes, ['autoupdater'], function (d) {
104
          if (d === null) {
105
            return null;
106
          } else if (d.enabled) {
107
            return d.branch;
108
          }
109
          return _.t('node.deactivated');
110
        });
111
112
        var siteDict = count(nodes, ['nodeinfo', 'site_code'], function (d) {
113
          if (config.siteNames) {
114
            config.siteNames.forEach(function (t) {
115
              if (d === t.site) {
116
                d = t.name;
117
              }
118
            });
119
          }
120
          return d;
121
        });
122
123
        statusTable = fillTable('node.status', statusTable, statusDict.sort(function (a, b) {
124
          return b[1] - a[1];
125
        }));
126
        fwTable = fillTable('node.firmware', fwTable, fwDict.sort(function (a, b) {
127
          if (b[0] < a[0]) {
128
            return -1;
129
          }
130
          if (b[0] > a[0]) {
131
            return 1;
132
          }
133
          return 0;
134
        }));
135
        hwTable = fillTable('node.hardware', hwTable, hwDict.sort(function (a, b) {
136
          return b[1] - a[1];
137
        }));
138
        geoTable = fillTable('node.visible', geoTable, geoDict.sort(function (a, b) {
139
          return b[1] - a[1];
140
        }));
141
        autoTable = fillTable('node.update', autoTable, autoDict.sort(function (a, b) {
142
          return b[1] - a[1];
143
        }));
144
        gatewayTable = fillTable('node.selectedGatewayIPv4', gatewayTable, gatewayDict.sort(function (a, b) {
145
          return b[1] - a[1];
146
        }));
147
        gateway6Table = fillTable('node.selectedGatewayIPv6', gateway6Table, gateway6Dict.sort(function (a, b) {
148
          return b[1] - a[1];
149
        }));
150
        siteTable = fillTable('node.site', siteTable, siteDict.sort(function (a, b) {
151
          return b[1] - a[1];
152
        }));
153
      };
154
155
      self.render = function render(el) {
156
        self.renderSingle(el, 'node.status', statusTable);
157
        self.renderSingle(el, 'node.firmware', fwTable);
158
        self.renderSingle(el, 'node.hardware', hwTable);
159
        self.renderSingle(el, 'node.visible', geoTable);
160
        self.renderSingle(el, 'node.update', autoTable);
161
        self.renderSingle(el, 'node.selectedGatewayIPv4', gatewayTable);
162
        self.renderSingle(el, 'node.selectedGatewayIPv6', gateway6Table);
163
        self.renderSingle(el, 'node.site', siteTable);
164
165
        if (config.globalInfos) {
166
          var images = document.createElement('div');
167
          el.appendChild(images);
168
          var img = [];
169
          config.globalInfos.forEach(function (globalInfo) {
170
            img.push(V.h('h2', globalInfo.name));
171
            img.push(helper.showStat(V, globalInfo));
172
          });
173
          V.patch(images, V.h('div', img));
174
        }
175
      };
176
177
      self.renderSingle = function renderSingle(el, heading, table) {
178
        if (table.children.length > 0) {
179
          var h2 = document.createElement('h2');
180
          h2.classList.add('proportion-header');
181
          h2.textContent = _.t(heading);
182
          h2.onclick = function onclick() {
183
            table.elm.classList.toggle('hide');
184
          };
185
          el.appendChild(h2);
186
          el.appendChild(table.elm);
187
        }
188
      };
189
      return self;
190
    };
191
  });
192